﻿using BunnyEater.Common.Const;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using System;
using System.IO;
using System.Reflection;
using System.Security.Cryptography.Pkcs;
using BunnyEater.WebAPI.Extensions;

namespace BunnyEater.WebAPI.Extensions
{
    /// <summary>
    /// 
    /// </summary>
    public static class SwaggerServiceRegister
    {
        /// <summary>
        /// Swagger注册配置
        /// </summary>
        /// <param name="serviceCollection"></param>
        /// <returns></returns>
        public static IServiceCollection SwaggerRegister(this IServiceCollection serviceCollection)
        {
            serviceCollection.AddSwaggerGen(option =>
            {
                //分组控制器中或控制器中的方法
                //option.SwaggerDoc("LoginManagement", new OpenApiInfo { Version = "v1", Title = "登录界面", Description = "登录界面" });
                //option.SwaggerDoc("SystemManage", new OpenApiInfo { Version = "v1", Title = "系统首页", Description = "系统首页" });
                //option.SwaggerDoc("XM", new OpenApiInfo { Version = "v1", Title = "系统首页", Description = "系统首页" });
                //
                option.SwaggerDoc("LOGIN", new OpenApiInfo { Version = "v1", Title = "登录界面", Description = "登录界面" });
                option.SwaggerDoc("SYSTEM", new OpenApiInfo { Version = "v1", Title = "系统管理", Description = "系统管理" });
                option.SwaggerDoc("TRACE", new OpenApiInfo { Version = "v1", Title = "溯源管理", Description = "溯源管理" });
                //option.SwaggerDoc("XTSY", new OpenApiInfo { Version = "v1", Title = "系统首页", Description = "系统首页" });
                //option.SwaggerDoc("WLWSB", new OpenApiInfo { Version = "v1", Title = "物联网设备", Description = "物联网设备" });
                //option.SwaggerDoc("QYSZ", new OpenApiInfo { Version = "v1", Title = "区域设置", Description = "区域设置" });
                //option.SwaggerDoc("RZGL", new OpenApiInfo { Version = "v1", Title = "日志管理", Description = "日志管理" });
                //option.SwaggerDoc("JKSJ", new OpenApiInfo { Version = "v1", Title = "监控数据", Description = "监控数据" });
                //option.SwaggerDoc("AQGL", new OpenApiInfo { Version = "v1", Title = "安全管理", Description = "安全管理" });
                //option.SwaggerDoc("ZCGL", new OpenApiInfo { Version = "v1", Title = "资产管理", Description = "资产管理" });
                //option.SwaggerDoc("YQGL", new OpenApiInfo { Version = "v1", Title = "园区管理", Description = "园区管理" });

                //强制 Swagger 使用 Guid.Empty
                option.SchemaFilter<GuidSchemaFilter>();


                //全局JWT认证 --切记：每个分组需要添加验证  输入 auth {token}（注意两者之间是一个空格） 
                option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = "每个分组需要添加验证。JWT授权(数据将在请求头中进行传输) 直接在下框中输入 auth {token}（注意两者之间是一个空格）,注意：用到那个文档就在哪个文档中输入验证",
                    Name = AssemblyConst.AUTHORIZATION,//jwt默认的参数名称AUTHORIZATION
                    In = ParameterLocation.Header,//jwt默认存放Authorization信息的位置(请求头中)
                    Type = SecuritySchemeType.ApiKey
                });
                option.AddSecurityRequirement(new OpenApiSecurityRequirement
                    {
                        { new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference()
                            {
                                Id = "Bearer",
                                Type = ReferenceType.SecurityScheme
                            }
                        }, Array.Empty<string>() }
                    });

                //为 Swagger JSON and UI设置xml文档注释路径
                var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);

                //获取应用程序所在目录（绝对路径，不受工作目录影响，建议采用此方法获取路径）
                var xmlPath = Path.Combine(basePath, "JWTCore.Api.xml");

                //如果需要显示控制器注释只需将第二个参数设置为true，此处报错时尝试打开WebApi启动项目=》属性=》生成=》输出=》勾选文档生成
                var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";

                option.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename), true);
            });

            return serviceCollection;
        }
    }
}
